<?php

namespace app\api\controller;

use app\common\exception\BusinessException;
use think\response\Json;

class Order extends Base
{
    /**
     * 创建订单
     * @return Json
     * @throws
     */
    public function create(): Json
    {
        // 外部参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'type' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'type.require' => '订单类型不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }

        // 提交数据
        $orderId = app(\app\api\logic\Order::class)->create($param);

        // 返回
        return $this->response(['order_id' => $orderId], 1, '提交成功');
    }

    /**
     * 查询订单
     * @return Json
     * @throws
     */
    public function info(): Json
    {
        // 外部参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'id' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'id.require' => '订单id不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }

        // 提交数据
        $response = app(\app\api\logic\Order::class)->orderInfo($param);

        // 返回
        return $this->response($response, 1, '提交成功');
    }

    /**
     * 订单支付
     * @return Json
     * @throws
     */
    public function pay(): Json
    {
        // 外部参数
        $param = $this->request->param();

        // 验证规则
        $rule = [
            'id' => 'require',
            //'pay_type' => 'require',
        ];

        // 错误信息
        $errMsg = [
            'id.require' => '订单id不得为空',
            //'pay_type.require' => '支付方式pay_type不得为空',
        ];

        // 数据验证
        $valRel = $this->validate($param, $rule, $errMsg);
        if ($valRel !== true) {
            throw new BusinessException($valRel, 0);
        }

        // 提交数据
        $response = app(\app\api\logic\Order::class)->orderPay($param);

        // 返回
        return $this->response($response, 1, '提交成功');
    }
}